home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’96
/
O Boy
/
Source
/
OBoy_hi.cp
< prev
next >
Wrap
Text File
|
1997-09-24
|
21KB
|
853 lines
/*
OBoyHI.h
© Bob Boylan 1996
Revision History
MacHack 1996 Initial creation
MH, Fri Morning pumped up the volume
*/
#include "OBoy_hi.h"
#include "O Boy.h"
#include "Helpers_ut.h"
#include "ComputerAEObj_pd.h"
#include "ComputerAETE_da.h"
#include "ProgressProc_hi.h"
#include "debug.h"
// file locals
static RGBColor theCoolGray = {0x4000, 0x4000, 0x4000};
static RGBColor theHotGray = {0x0800, 0x0800, 0x0800};
static RGBColor theBlack = {0xFFFF, 0xFFFF, 0xFFFF};
const Int_16 kButtonWidth = 8;
const Int_16 kButtonHeight = 8;
// class statics
Boolean OBoy_hi::_CancelWasIssued;
RGBColor OBoy_hi::_BackGroundGray = {0xE605, 0xE605, 0xE605};
// -----------------------------------------------------------------
// ctor
//
OBoy_hi::OBoy_hi()
: _UpNavButton( nil ),
_DownNavButton( nil ),
_LeftNavButton( nil ),
_RightNavButton( nil ),
_MaxItemsToShow( 10 )
{
// show the window
_WindowP = GetNewDialog( 128, nil, (WindowPtr)-1L );
SetWRefCon(_WindowP, (Int_32) this);
// init the list
{
Int_16 theItemType;
Handle theItemHandle;
Rect theItemRect;
Rect theListBounds = {0,0,0,0};
Point theCellSize = {0,0};
GetDialogItem((DialogPtr)_WindowP, 1, &theItemType, &theItemHandle, &theItemRect);
theItemRect.right -= 16;
_ListH = LNew( &theItemRect, &theListBounds,
theCellSize, 0, _WindowP,
true, false, false, true);
(*_ListH)->selFlags |= lOnlyOne;
LAddColumn( 1, 0, _ListH );
}
// init the nav buttons
UpdateNavRegions();
// progress setup
_TimeOfLastProgressUpdate = TickCount();
_Progress = 0;
_UpdateInProgress = false;
_CancelWasIssued = false;
}
// -----------------------------------------------------------------
// dtor
//
OBoy_hi::~OBoy_hi()
{
LDispose( _ListH );
DisposeWindow( _WindowP );
DisposeRgn(_UpNavButton);
DisposeRgn(_DownNavButton);
DisposeRgn(_LeftNavButton);
DisposeRgn(_RightNavButton);
}
// -----------------------------------------------------------------
// DrawHierNavButton
//
void
OBoy_hi::DrawHierNavButton( OBoy_hi::EHButton inButtonToDraw , Boolean inHighlight )
{
const kRoundness = 4;
// get the bounds
Rect theWholeRect;
{
Handle theItemH;
Int_16 theItemType;
GetDialogItem(_WindowP, 2, &theItemType, &theItemH, &theWholeRect);
}
Int_16 theRectWidth = theWholeRect.right-theWholeRect.left;
Int_16 theRectHeight = theWholeRect.bottom-theWholeRect.top;
Point theCenter;
Rect theRect;
StPort thePort( _WindowP );
StPen thePen;
// setup the foregr/backgr color
if( inHighlight == true )
{
RGBForeColor( &theHotGray );
RGBBackColor( &theHotGray );
}
else
{
RGBForeColor( &theCoolGray );
RGBBackColor( &theCoolGray );
}
// calc center
theCenter.h = theWholeRect.left + (theRectWidth)/2;
theCenter.v = theWholeRect.top + (theRectHeight)/2;
// up button
if( inButtonToDraw & UpHButton )
{
SetRect(&theRect, theCenter.h-kButtonWidth, theWholeRect.top,
theCenter.h+kButtonWidth, theCenter.v);
if( inHighlight ) theRect.bottom -= kButtonWidth;
PaintRoundRect( &theRect,kRoundness,kRoundness );
}
// down button
if( inButtonToDraw & DownHButton )
{
SetRect(&theRect, theCenter.h-kButtonWidth, theCenter.v,
theCenter.h+kButtonWidth, theWholeRect.bottom);
if( inHighlight ) theRect.top += kButtonWidth;
PaintRoundRect( &theRect,kRoundness,kRoundness );
}
// left button
if( inButtonToDraw & LeftHButton )
{
SetRect(&theRect, theWholeRect.left,theCenter.v-kButtonHeight,
theCenter.h, theCenter.v+kButtonHeight );
if( inHighlight ) theRect.right -= kButtonWidth;
PaintRoundRect( &theRect,kRoundness,kRoundness );
}
// right button
if( inButtonToDraw & RightHButton )
{
SetRect(&theRect, theCenter.h, theCenter.v-kButtonHeight,
theWholeRect.right,theCenter.v+kButtonHeight );
if( inHighlight ) theRect.left += kButtonWidth;
PaintRoundRect( &theRect,kRoundness,kRoundness );
}
// the middle
DrawO( 0 );
}
// -----------------------------------------------------------------
// UpdateProgress
//
void
OBoy_hi::UpdateProgress()
{
// throttle the updates
if( TickCount() > (_TimeOfLastProgressUpdate + 15L) )
{
_TimeOfLastProgressUpdate = TickCount();
_Progress = (_Progress + 1) & 1;
DrawO( _Progress + 1 );
}
}
// -----------------------------------------------------------------
// DrawO
//
void
OBoy_hi::DrawO( Int_32 inStage )
{
StPort thePort( _WindowP );
StPen thePen;
if( inStage == 0 )
{
RGBForeColor( &_BackGroundGray );
RGBBackColor( &_BackGroundGray );
}
else if( inStage == 1 )
{
RGBForeColor( &theCoolGray );
RGBBackColor( &theCoolGray );
}
else
{
RGBForeColor( &theHotGray );
RGBBackColor( &theHotGray );
}
Rect theWholeRect;
{
Handle theItemH;
Int_16 theItemType;
GetDialogItem(_WindowP, 2, &theItemType, &theItemH, &theWholeRect);
}
InsetRect( &theWholeRect, (theWholeRect.right-theWholeRect.left)/2 - kButtonWidth + 1,
(theWholeRect.right-theWholeRect.left)/2 - kButtonWidth + 1 );
PaintOval( &theWholeRect );
}
// -----------------------------------------------------------------
// ReDraw
//
void
OBoy_hi::ReDraw()
{
Int_16 theItemType;
Handle theItemH;
Rect theRect;
// save current state
StPort thePort( _WindowP );
StPen thePen;
thePen.Reset();
// hier navigator
DrawHierNavButton();
// 'a' button
GetDialogItem(_WindowP, 3, &theItemType, &theItemH, &theRect);
RGBForeColor( &theCoolGray );
RGBBackColor( &theCoolGray );
PaintOval( &theRect );
// 'b' button
GetDialogItem(_WindowP, 4, &theItemType, &theItemH, &theRect);
PaintOval( &theRect );
// the list
thePen.Reset();
GetDialogItem(_WindowP, 1, &theItemType, &theItemH, &theRect);
EraseRect( &theRect );
LUpdate( _WindowP->visRgn, _ListH );
GetDialogItem(_WindowP, 1, &theItemType, &theItemH, &theRect);
InsetRect( &theRect, -1, -1 );
FrameRect( &theRect );
if( _Obj.Isnil() == true )
{
AEObj_pd * theObjP = new ComputerAEObj_pd;
_Obj = Clone_ut<AEObj_pd> (theObjP);
UpdateObjectData();
}
else
{
UpdateName();
}
// the grow job
{
RgnHandle theRgn = NewRgn();
GetClip ( theRgn );
Rect theRect;
SetRect( &theRect,_WindowP->portRect.right-15,
_WindowP->portRect.bottom-15,
_WindowP->portRect.right,
_WindowP->portRect.bottom );
ClipRect( &theRect );
DrawGrowIcon( _WindowP );
SetClip( theRgn );
DisposeRgn( theRgn );
}
}
// -----------------------------------------------------------------
// UpdateName ... the text box indicating the path to an object
//
void
OBoy_hi::UpdateName()
{
Int_16 theItemType;
Handle theItemH;
Rect theRect;
string theName = (*_Obj).GetFullName();
StPort thePort( _WindowP );
GetDialogItem(_WindowP, 5, &theItemType, &theItemH, &theRect);
StPen thePen;
thePen.Reset();
RGBBackColor( &_BackGroundGray );
TextFont( applFont );
TextSize( 9 );
TETextBox(theName.c_str(), theName.length(), &theRect, teFlushLeft);
TextFont( systemFont );
TextSize( 12 );
}
// -----------------------------------------------------------------
// UpdateObjectData
//
void
OBoy_hi::UpdateObjectData()
{
ProgressProc_hi theProgress( this );
// start fresh
{
vector< Clone_ut<AEObj_pd> > theNullObjSet;
_SubModels = theNullObjSet;
}
{
vector< Clone_ut<PropertyValue_pd> > theNullPropSet;
_PropertyValues = theNullPropSet;
}
// update the name, then the object data, then the name ( could have changed)
_UpdateInProgress = true;
UpdateName();
(*_Obj).Update( theProgress, _MaxItemsToShow );
UpdateName();
_UpdateInProgress = false;
DrawO( 0 );
_CancelWasIssued = false;
}
// -----------------------------------------------------------------
// NewSubModel
//
void
OBoy_hi::NewSubModel( Clone_ut< AEObj_pd > inAEobj )
{
_SubModels.push_back( inAEobj );
UpdateProgress();
}
// -----------------------------------------------------------------
// NewPropertyValue
//
void
OBoy_hi::NewPropertyValue( Clone_ut< PropertyValue_pd > inPropertyValue )
{
_PropertyValues.push_back( inPropertyValue );
UpdateProgress();
}
// -----------------------------------------------------------------
// HandleNavButton
//
void
OBoy_hi::HandleNavButton(EventRecord * inEventP, Point inLocalWhere)
{
EHButton theButton = FindHButton( inLocalWhere );
EHButton thePointIn, theLastPointIn;
Point thePoint, theLastPoint;
// see what the user is going to do
thePoint = inLocalWhere;
theLastPoint.h = 0; theLastPoint.v = 0;
while( StillDown() )
{
GetMouse( &thePoint );
if( (thePoint.h != theLastPoint.h) || (thePoint.v != theLastPoint.v ) )
{
thePointIn = FindHButton( thePoint );
theLastPointIn = FindHButton( theLastPoint );
if( thePointIn != theLastPointIn )
{
DrawHierNavButton( theButton, (thePointIn == theButton) );
}
theLastPoint = thePoint;
}
}
// act, restore image
if( thePointIn == theButton )
{
NavigateHier( theButton, inEventP );
DrawHierNavButton( theButton);
}
}
// -----------------------------------------------------------------
// FindHButton
//
OBoy_hi::EHButton
OBoy_hi::FindHButton( Point inLocation )
{
EHButton theRetVal;
if( PtInRgn( inLocation,_UpNavButton ) )
{
theRetVal = UpHButton;
}
else if ( PtInRgn( inLocation, _DownNavButton ) )
{
theRetVal = DownHButton ;
}
else if ( PtInRgn( inLocation, _LeftNavButton ) )
{
theRetVal = LeftHButton ;
}
else if ( PtInRgn( inLocation, _RightNavButton ) )
{
theRetVal = RightHButton ;
}
else
{
theRetVal = NoHButton ;
}
return theRetVal;
}
// -----------------------------------------------------------------
// NavigateHier
//
void
OBoy_hi::NavigateHier( OBoy_hi::EHButton inDirection, EventRecord *inEventP )
{
Cell theCell = LLastClick( _ListH );
Boolean theCommandKeyDown = (inEventP->modifiers & cmdKey) != 0 ;
Boolean theOptionKeyDown = (inEventP->modifiers & optionKey) != 0 ;
// which way was the request?
switch( inDirection )
{
case UpHButton:
{
if( (*_Obj).GetKindID() != cComputer )
{
_Obj = (*_Obj).GetParent();
if( theCommandKeyDown == true )
{ // all the way up
while( (*_Obj).GetKindID() != cComputer )
{
_Obj = (*_Obj).GetParent();
}
}
}
}
break;
case DownHButton:
{
// see what the user has selected - could be a property
if( theCell.v >= _PropertyValues.size() )
{
Int_32 theIndex = theCell.v - _PropertyValues.size();
if( theIndex < _SubModels.size() )
{
_Obj = _SubModels[ theIndex ];
}
else
{
if( _SubModels.size() > 0 )
{
_Obj = _SubModels[ 0 ];
}
}
}
}
break;
case LeftHButton:
{
if( (*_Obj).GetKindID() != cComputer )
{
Int_32 theLeftLimit;
if( (*_Obj).GetKindID() == cFinderProcess )
{
theLeftLimit = 0;
}
else
{
theLeftLimit = 1;
}
if ( (*_Obj).GetPosition() > theLeftLimit )
{
Int_32 theDelta = -1;
if( theCommandKeyDown == true )
{ // go all the way to the left
theDelta = -((*_Obj).GetPosition() - theLeftLimit);
}
else if( theOptionKeyDown == true )
{ // go in steps of _MaxItemsToShow, start at a value divisible by _MaxItemsToShow
Int_32 thePositionWeWant = ((*_Obj).GetPosition()/_MaxItemsToShow)*_MaxItemsToShow;
if( thePositionWeWant == (*_Obj).GetPosition() )
{
thePositionWeWant = thePositionWeWant - _MaxItemsToShow;
}
if( thePositionWeWant < _MaxItemsToShow )
{
theDelta = -((*_Obj).GetPosition() - theLeftLimit);
}
else
{
theDelta = -((*_Obj).GetPosition() - thePositionWeWant);
}
}
_Obj = (*_Obj).GetSibling( theDelta );
}
}
}
break;
case RightHButton:
{
if( (*_Obj).GetKindID() != cComputer )
{
Int_32 theDelta = 1;
if( theCommandKeyDown == true )
{ // go all the way to the right
Clone_ut<AEObj_pd> theParent = (*_Obj).GetParent();
Int_32 theNSubs = (*theParent).GetSubModelCount( (*_Obj).GetKindID() );
if( theNSubs > (*_Obj).GetPosition() )
{
theDelta = theNSubs - (*_Obj).GetPosition();
}
else
{
theDelta = (*_Obj).GetPosition() - theNSubs;
}
}
else if( theOptionKeyDown == true )
{ // go in steps of _MaxItemsToShow
Int_32 thePositionWeWant = ((*_Obj).GetPosition()/_MaxItemsToShow)*_MaxItemsToShow + _MaxItemsToShow;
theDelta = thePositionWeWant - (*_Obj).GetPosition();
}
if( theDelta > 0 )
{
_Obj = (*_Obj).GetSibling( theDelta );
}
}
}
break;
}
// all done deciding who gets it ... now give it to them
UpdateObjectData();
}
// -----------------------------------------------------------------
// HandleClick
//
void
OBoy_hi::HandleClick(EventRecord * inEventP)
{
Point thelocalWhere;
StPort thePort( _WindowP );
thelocalWhere = inEventP->where;
GlobalToLocal(&thelocalWhere);
ControlHandle theControlH;
Int_16 thePartCode = FindDialogItem(_WindowP, thelocalWhere) + 1;
switch( thePartCode )
{
case 1: // the list
{
StPen thePen;
thePen.Reset();
Boolean theDblClickFlag = LClick( thelocalWhere, inEventP->modifiers, _ListH );
if( (_UpdateInProgress == false ) && (theDblClickFlag == true) )
{
NavigateHier( DownHButton, inEventP );
}
}
break;
case 2: // the nav button
if( _UpdateInProgress == false )
{
HandleNavButton( inEventP, thelocalWhere );
}
break;
case 3: // the 'a' button
if( TrackButtonHit( thePartCode ) == true )
{
if( _UpdateInProgress == false )
{
if ( ((inEventP->modifiers & cmdKey) !=0) && (_MaxItemsToShow == 10) )
{
_MaxItemsToShow = 11; // pump up the volume - this is MacHack 11 (Spinal Hack)
}
UpdateObjectData();
}
}
break;
case 4: // the 'b' button
if( TrackButtonHit( thePartCode ) == true )
{
ShowAbout();
}
break;
default:
break;
}
}
// -----------------------------------------------------------------
// HandleKeyDownEvent
//
void
OBoy_hi::HandleKeyDownEvent(EventRecord &theEvent)
{
char theKey;
char theVirtualKey;
theKey=theEvent.message & charCodeMask;
theVirtualKey = (theEvent.message & keyCodeMask) >> 8;
if ( (theEvent.modifiers & cmdKey) !=0 )
{
EHButton theNavButton = NoHButton;
switch( theKey )
{
case '.':
_CancelWasIssued = true; // ahhh ... cancel
break;
default:
break;
}
}
}
// -----------------------------------------------------------------
// TrackButtonHit
//
Boolean
OBoy_hi::TrackButtonHit( Int_16 inButtonItem )
{
Int_16 theItemType;
Handle theItemH;
Rect theRect;
StPort thePort( _WindowP );
StPen theBigPen;
// get the rect involved
GetDialogItem(_WindowP, inButtonItem, &theItemType, &theItemH, &theRect);
InsetRect( &theRect,1,1 );
Boolean thePointIn;
{
StPen thePen; // save the current
// hang out till the user mouses up
Point thePoint, theLastPoint;
Boolean theLastPointIn;
theLastPoint.h = 0; theLastPoint.v = 0;
while( StillDown() )
{
GetMouse( &thePoint );
if( (thePoint.h != theLastPoint.h) || (thePoint.v != theLastPoint.v ) )
{
thePointIn = PtInRect( thePoint, &theRect );
theLastPointIn = PtInRect( theLastPoint, &theRect );
if( thePointIn != theLastPointIn )
{
if( thePointIn )
{
RGBForeColor( &theHotGray );
RGBBackColor( &theHotGray );
PaintOval( &theRect );
}
else
{
RGBForeColor( &theCoolGray );
RGBBackColor( &theCoolGray );
PaintOval( &theRect );
}
}
theLastPoint = thePoint;
}
}
}
// restore image
if( thePointIn == true )
{
RGBForeColor( &theCoolGray );
RGBBackColor( &theCoolGray );
PaintOval( &theRect );
}
// and the result
return thePointIn;
}
// -----------------------------------------------------------------
// ShowAbout
//
void
OBoy_hi::ShowAbout()
{
StPort thePort;
DialogPtr theDialogP = GetNewDialog( 129, nil, (WindowPtr)-1L );
SetPort( theDialogP );
Int_16 theItemType;
Handle theItemHandle;
Rect theItemRect, theTextItemRect;
// get the popup control
GetDialogItem((DialogPtr)theDialogP, 2, &theItemType, &theItemHandle, &theItemRect);
ControlHandle thePopupControlH = (ControlHandle) theItemHandle;
SetControlValue( thePopupControlH, 2 ); // default is what
// get the text item rect
GetDialogItem((DialogPtr)theDialogP, 3, &theItemType, &theItemHandle, &theTextItemRect);
Int_16 theItemHit = 2;
while( theItemHit != 1 )
{
if( theItemHit == 2 )
{
Int_16 theItemNumber = GetControlValue( thePopupControlH );
dassert( theItemNumber > 0 );
Handle theTextH = GetResource( 'TEXT', 999 + theItemNumber );
HLock( theTextH );
TextFont( monaco );
TextSize( 9 );
TETextBox( *theTextH, GetHandleSize( theTextH ), &theTextItemRect, teFlushLeft );
TextFont( systemFont );
TextSize( 12 );
HUnlock( theTextH );
ReleaseResource( theTextH );
}
ModalDialog( nil, &theItemHit );
}
DisposeDialog( theDialogP );
}
// -----------------------------------------------------------------
// AdjustToSize
//
void
OBoy_hi::AdjustToSize( Rect theOldRect, Rect theNewRect )
{
Int_16 theDeltaX = theNewRect.right - theOldRect.right;
Int_16 theDeltaY = theNewRect.bottom - theOldRect.bottom;
Int_16 theItemType;
Handle theItemHandle;
Rect theItemRect;
StPort theSavePort( _WindowP );
// adjust the list item
GetDialogItem((DialogPtr)_WindowP, 1, &theItemType, &theItemHandle, &theItemRect);
theItemRect.right += theDeltaX;
theItemRect.bottom += theDeltaY;
SetDialogItem((DialogPtr)_WindowP, 1, theItemType, theItemHandle, &theItemRect);
LSize((theItemRect.right - theItemRect.left) - 15,
(theItemRect.bottom - theItemRect.top), _ListH );
Point theCellSize = (**_ListH).cellSize;
theCellSize.h = (theItemRect.right - theItemRect.left) - 15;
LCellSize ( theCellSize, _ListH);
// adjust the nav button
GetDialogItem((DialogPtr)_WindowP, 2, &theItemType, &theItemHandle, &theItemRect);
OffsetRect( &theItemRect, 0, theDeltaY );
SetDialogItem((DialogPtr)_WindowP, 2, theItemType, theItemHandle, &theItemRect);
UpdateNavRegions();
// adjust the 'a' button
GetDialogItem((DialogPtr)_WindowP, 3, &theItemType, &theItemHandle, &theItemRect);
OffsetRect( &theItemRect, theDeltaX, theDeltaY );
SetDialogItem((DialogPtr)_WindowP, 3, theItemType, theItemHandle, &theItemRect);
// adjust the 'b' button
GetDialogItem((DialogPtr)_WindowP, 4, &theItemType, &theItemHandle, &theItemRect);
OffsetRect( &theItemRect, theDeltaX, theDeltaY );
SetDialogItem((DialogPtr)_WindowP, 4, theItemType, theItemHandle, &theItemRect);
// adjust the text item
GetDialogItem((DialogPtr)_WindowP, 5, &theItemType, &theItemHandle, &theItemRect);
theItemRect.right += theDeltaX;
theItemRect.top += theDeltaY;
theItemRect.bottom += theDeltaY;
SetDialogItem((DialogPtr)_WindowP, 5, theItemType, theItemHandle, &theItemRect);
// update
InvalRect( &_WindowP->portRect );
}
// -----------------------------------------------------------------
// UpdateNavRegions
//
void
OBoy_hi::UpdateNavRegions()
{
if( _UpNavButton != nil )
{
DisposeRgn(_UpNavButton);
DisposeRgn(_DownNavButton);
DisposeRgn(_LeftNavButton);
DisposeRgn(_RightNavButton);
}
_UpNavButton = NewRgn();
_DownNavButton = NewRgn();
_LeftNavButton = NewRgn();
_RightNavButton = NewRgn();
Handle theItemH;
Int_16 theItemType;
Rect theRect;
Point theCenter;
GetDialogItem(_WindowP, 2, &theItemType, &theItemH, &theRect);
theCenter.h = theRect.left + (theRect.right-theRect.left)/2;
theCenter.v = theRect.top + (theRect.bottom-theRect.top)/2;
OpenRgn();
MoveTo( theCenter.h, theCenter.v );
LineTo( theRect.right, theRect.top );
LineTo( theRect.left, theRect.top );
LineTo( theCenter.h, theCenter.v );
CloseRgn( _UpNavButton );
OpenRgn();
MoveTo( theCenter.h, theCenter.v );
LineTo( theRect.right, theRect.bottom );
LineTo( theRect.left, theRect.bottom );
LineTo( theCenter.h, theCenter.v );
CloseRgn( _DownNavButton );
OpenRgn();
MoveTo( theCenter.h, theCenter.v );
LineTo( theRect.left, theRect.top );
LineTo( theRect.left, theRect.bottom );
LineTo( theCenter.h, theCenter.v );
CloseRgn( _LeftNavButton );
OpenRgn();
MoveTo( theCenter.h, theCenter.v );
LineTo( theRect.right, theRect.top );
LineTo( theRect.right, theRect.bottom );
LineTo( theCenter.h, theCenter.v );
CloseRgn( _RightNavButton );
}